﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Hyldahl.Services.TaskProcessing;

namespace TaskCreator
{
    public partial class CreateTaskForm : Form
    {
        public CreateTaskForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Validating event of the taskDataTextBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void taskDataTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(taskDataTextBox.Text))
            {
                errorProvider.SetError(taskDataTextBox, "No task data");
                e.Cancel = true;
            }
        }

        /// <summary>
        /// Handles the Validating event of the taskTypeComboBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void taskTypeComboBox_Validating(object sender, CancelEventArgs e)
        {
            if (taskTypeComboBox.SelectedItem == null)
            {
                errorProvider.SetError(taskTypeComboBox, "No task type");
                e.Cancel = true;
            }
        }

        /// <summary>
        /// Handles the Click event of the createTaskButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void createTaskButton_Click(object sender, EventArgs e)
        {
            if (!this.Validate())
                return;

            int numberOfTasks = int.Parse((string)numberOfTasksComboBox.SelectedItem);

            for (int idx = 0; idx < numberOfTasks; idx++)
            {
                Task task = new Task();

                task.TaskType = (string)taskTypeComboBox.SelectedItem;
                task.Data = taskDataTextBox.Text;

                TaskProcessingApplicationContext.Current.DataStore.CreateTask(task);
            }

            MessageBox.Show(string.Format("{0} task{1} Created.", numberOfTasks, numberOfTasks > 1 ? "s" : ""));

            taskDataTextBox.Text = string.Empty;
        }

        private void CreateTaskForm_Load(object sender, EventArgs e)
        {
            taskTypeComboBox.Items.Clear();

            foreach(TaskProcessor tp in TaskProcessingApplicationContext.Current.TaskProcessorPlugins.TaskProcessors)
            {
                taskTypeComboBox.Items.Add(tp.TaskType);
            }

            taskTypeComboBox.Items.Add("Unknown Task");

            taskTypeComboBox.SelectedIndex = 0;
            numberOfTasksComboBox.SelectedIndex = 0;
        }
    }
}
